﻿using System;
using Webshop20.AntiCorruptionLayers.Quartz.Services.Contracts;
using Webshop20.AntiCorruptionLayers.Quartz.Facades.Contracts;
using Webshop20.AntiCorruptionLayers.Quartz.Translators;

namespace Webshop20.AntiCorruptionLayers.Quartz.Services.Implementations
{
    public class SchedulingService  :  ISchedulingService
    {
        private readonly ISchedulerFacade schedulerFacade;
        private readonly IJobDetailTranslator jobDetailTranslator;
        private readonly ISimpleTriggerTranslator simpleTriggerTranslator;

        public SchedulingService(ISchedulerFacade schedulerFacade, IJobDetailTranslator jobDetailTranslator, ISimpleTriggerTranslator simpleTriggerTranslator)
        {
            this.schedulerFacade = schedulerFacade;
            this.simpleTriggerTranslator = simpleTriggerTranslator;
            this.jobDetailTranslator = jobDetailTranslator;
        }

        public void SetNewPointOfTimeForProcessContinuation(int processID, DateTime timeToResurrect, Type eventType)
        {
            var jobDetail = jobDetailTranslator.ConvertToJobDetail(processID, eventType);
            var trigger = simpleTriggerTranslator.ConvertToTrigger(timeToResurrect);
            schedulerFacade.ScheduleProcessResurrection(jobDetail, trigger);
        }
    }
}
